#include "SolidBlockShader.h"
#include "base.h"

SolidBlockShader::SolidBlockShader(void)
{
}
SolidBlockShader::~SolidBlockShader(void)
{
	glDeleteSamplers(1, &m_texSampler);
}
string SolidBlockShader::GetUniqueShaderName()
{
	return "blockSolid";
}
bool SolidBlockShader::Initialize()
{
	if(!BaseShaderProgram::Initialize("blocksolid.vs", "blocksolid.fs")) 
			return false;

	//Setup sampler
	glGenSamplers(1, &m_texSampler);
	glSamplerParameteri(m_texSampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glSamplerParameteri(m_texSampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glSamplerParameteri(m_texSampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glSamplerParameteri(m_texSampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	uf_texSampler = glGetUniformLocation(m_nProgramName, "sampler");
	uf_matWorld = glGetUniformLocation(m_nProgramName, "world");
	uf_matView = glGetUniformLocation(m_nProgramName, "view");
	uf_matProjection = glGetUniformLocation(m_nProgramName, "projection");
	uf_textureIndex = glGetUniformLocation(m_nProgramName, "blockTextureIndex");

	in_position = glGetAttribLocation(m_nProgramName, "position");
	in_normal = glGetAttribLocation(m_nProgramName, "normal");
	in_texcoord = glGetAttribLocation(m_nProgramName, "texcoord");

	return true;
}
void SolidBlockShader::SetTexture(const Texture *t)
{
	glEnable(GL_TEXTURE_2D);

	//Bind Texture to sampler
	glBindSampler(0, m_texSampler);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, t->GetTextureName());

	//Bind to sampler at unit 0
	glUniform1i(uf_texSampler, 0);
}
void SolidBlockShader::SetProjection(const matrix &m)
{
	glUniformMatrix4fv(uf_matProjection, 1, g_bMatrixTransposed, m.DataPtr());
}
void SolidBlockShader::SetView(const matrix &m)
{
	glUniformMatrix4fv(uf_matView, 1, g_bMatrixTransposed, m.DataPtr());
}
void SolidBlockShader::SetWorld(const matrix &m)
{
	glUniformMatrix4fv(uf_matWorld, 1, g_bMatrixTransposed, m.DataPtr());
}
void SolidBlockShader::SetTextureIndex(const vector2 &texIndex)
{
	glUniform2f(uf_textureIndex, texIndex.x, texIndex.y);
}
int SolidBlockShader::GetVertexLocation() const
{
	return in_position;
}
int SolidBlockShader::GetNormalLocation() const
{
	return in_normal;
}
int SolidBlockShader::GetTexcoordLocation() const
{
	return in_texcoord;
}